<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<meta http-equiv="x-UA-Compatible" content="ie=Edge">
   	<meta name="viewport" content="width=device-width, initial-scale=1">
   	<title>Crafty - Crafty.map</title>
	<link type="text/css" rel="stylesheet" href="../craftyjs-site.css" />
	<link rel="shortcut icon" href="../favicon.ico">
</head>
<body>
	<div id = "header-background"> </div>
	<div id = "page-frame">
	<div id="header">
		<nav class="navbar">
  			<div class="container-fluid">
  				<div class="navbar-header">
  					<a class="navbar-brand" href="../"> <img class="logo" src="../images/text-logo.png" /> </a>
  					
				</div>
				<div class="collapse navbar-collapse navbar-right" id="bs-example-navbar-collapse-1">
					<ul class="nav navbar-nav">
						<li><a href="../">首页</a></li>
						<li><a href="../getting-started/">快速开始</a></li>
						<li><a href="../documentation/">文档</a></li>
						<li><a href="../api/">API</a></li>
						<li><a href="../components/">组件</a></li>
						<li class="emph"><a href="../#install">下载</a></li>
					</ul>
				</div>
			</div>
		</nav>
	</div>


	<div id="main">
		<div id="content" class="container">
			



<div id="docs"><div class="toc-holder" id="doc-nav"><ul id="doc-level-one"><li><a href="events.html">List of Events</a></li><li class="category">Core<ul class="category-list"><li><a href="Crafty.html">Crafty</a></li><li><a href="Crafty Core.html">Crafty Core</a></li><li><a href="Crafty-bind.html">Crafty.bind()</a></li><li><a href="Crafty-c.html">Crafty.c()</a></li><li><a href="Crafty-clone.html">Crafty.clone()</a></li><li><a href="Crafty-defineField.html">Crafty.defineField()</a></li><li><a href="Crafty-e.html">Crafty.e()</a></li><li><a href="Crafty-extend.html">Crafty.extend()</a></li><li><a href="Crafty-frame.html">Crafty.frame()</a></li><li><a href="Crafty-getVersion.html">Crafty.getVersion()</a></li><li><a href="Crafty-init.html">Crafty.init()</a></li><li><a href="Crafty-isPaused.html">Crafty.isPaused()</a></li><li><a href="Crafty-one.html">Crafty.one()</a></li><li><a href="Crafty-pause.html">Crafty.pause()</a></li><li><a href="Crafty-s.html">Crafty.s()</a></li><li><a href="Crafty-settings.html">Crafty.settings</a></li><li><a href="Crafty-stage.html">Crafty.stage</a></li><li><a href="Crafty-stop.html">Crafty.stop()</a></li><li><a href="Crafty-support.html">Crafty.support</a></li><li><a href="Crafty-trigger.html">Crafty.trigger()</a></li><li><a href="Crafty-unbind.html">Crafty.unbind()</a></li><li><a href="Crafty-uniqueBind.html">Crafty.uniqueBind()</a></li></ul></li><li class="category">2D<ul class="category-list"><li><a href="2D.html">2D</a></li><li><a href="AngularMotion.html">AngularMotion</a></li><li><a href="Collision.html">Collision</a></li><li><a href="Crafty-circle.html">Crafty.circle</a></li><li><a href="Crafty-diamondIso.html">Crafty.diamondIso</a></li><li><a href="Crafty-HashMap.html">Crafty.HashMap</a></li><li><a href="Crafty-isometric.html">Crafty.isometric</a></li><li><a href="Crafty-map.html">Crafty.map</a></li><li><a href="Crafty-math-Matrix2D.html">Crafty.math.Matrix2D</a></li><li><a href="Crafty-math-Vector2D.html">Crafty.math.Vector2D</a></li><li><a href="Crafty-polygon.html">Crafty.polygon</a></li><li><a href="Crafty-raycast.html">Crafty.raycast()</a></li><li><a href="Crafty-rectManager.html">Crafty.rectManager</a></li><li><a href="Gravity.html">Gravity</a></li><li><a href="GroundAttacher.html">GroundAttacher</a></li><li><a href="Motion.html">Motion</a></li><li><a href="Supportable.html">Supportable</a></li></ul></li><li class="category">Animation<ul class="category-list"><li><a href="Crafty-easing.html">Crafty.easing</a></li><li><a href="SpriteAnimation.html">SpriteAnimation</a></li><li><a href="Tween.html">Tween</a></li></ul></li><li class="category">Assets<ul class="category-list"><li><a href="Crafty-asset.html">Crafty.asset()</a></li><li><a href="Crafty-assets.html">Crafty.assets</a></li><li><a href="Crafty-imageWhitelist.html">Crafty.imageWhitelist()</a></li><li><a href="Crafty-load.html">Crafty.load()</a></li><li><a href="Crafty-paths.html">Crafty.paths()</a></li><li><a href="Crafty-removeAssets.html">Crafty.removeAssets()</a></li></ul></li><li class="category">Audio<ul class="category-list"><li><a href="Crafty-audio.html">Crafty.audio</a></li></ul></li><li class="category">Controls<ul class="category-list"><li><a href="Controllable.html">Controllable</a></li><li><a href="Controls.html">Controls</a></li><li><a href="Draggable.html">Draggable</a></li><li><a href="Fourway.html">Fourway</a></li><li><a href="Jumper.html">Jumper</a></li><li><a href="Multiway.html">Multiway</a></li><li><a href="Twoway.html">Twoway</a></li></ul></li><li class="category">Debug<ul class="category-list"><li><a href="Crafty-error.html">Crafty.error()</a></li><li><a href="Crafty-log.html">Crafty.log()</a></li><li><a href="DebugCanvas.html">DebugCanvas</a></li><li><a href="DebugPolygon.html">DebugPolygon</a></li><li><a href="DebugRectangle.html">DebugRectangle</a></li><li><a href="SolidAreaMap.html">SolidAreaMap</a></li><li><a href="SolidHitBox.html">SolidHitBox</a></li><li><a href="VisibleMBR.html">VisibleMBR</a></li><li><a href="WiredAreaMap.html">WiredAreaMap</a></li><li><a href="WiredHitBox.html">WiredHitBox</a></li></ul></li><li class="category">Events<ul class="category-list"><li><a href="Crafty-addEvent.html">Crafty.addEvent()</a></li><li><a href="Crafty-bind.html">Crafty.bind()</a></li><li><a href="Crafty-one.html">Crafty.one()</a></li><li><a href="Crafty-removeEvent.html">Crafty.removeEvent()</a></li><li><a href="Crafty-trigger.html">Crafty.trigger()</a></li><li><a href="Crafty-unbind.html">Crafty.unbind()</a></li><li><a href="Crafty-uniqueBind.html">Crafty.uniqueBind()</a></li></ul></li><li class="category">Game Loop<ul class="category-list"><li><a href="Crafty-timer.html">Crafty.timer</a></li></ul></li><li class="category">Graphics<ul class="category-list"><li><a href="Canvas.html">Canvas</a></li><li><a href="CanvasLayer.html">CanvasLayer</a></li><li><a href="Color.html">Color</a></li><li><a href="Crafty-assignColor.html">Crafty.assignColor()</a></li><li><a href="Crafty-background.html">Crafty.background()</a></li><li><a href="Crafty-createLayer.html">Crafty.createLayer()</a></li><li><a href="Crafty-defaultShader.html">Crafty.defaultShader()</a></li><li><a href="Crafty-domHelper.html">Crafty.domHelper</a></li><li><a href="Crafty-pixelart.html">Crafty.pixelart()</a></li><li><a href="Crafty-sprite.html">Crafty.sprite()</a></li><li><a href="Crafty-WebGLShader.html">Crafty.WebGLShader()</a></li><li><a href="DOM.html">DOM</a></li><li><a href="DomLayer.html">DomLayer</a></li><li><a href="HTML.html">HTML</a></li><li><a href="Image.html">Image</a></li><li><a href="Particles.html">Particles</a></li><li><a href="Renderable.html">Renderable</a></li><li><a href="Sprite.html">Sprite</a></li><li><a href="Text.html">Text</a></li><li><a href="WebGL.html">WebGL</a></li><li><a href="WebGLLayer.html">WebGLLayer</a></li></ul></li><li class="category">Input<ul class="category-list"><li><a href="AreaMap.html">AreaMap</a></li><li><a href="Button.html">Button</a></li><li><a href="Crafty-findPointerEventTargetByComponent.html">Crafty.findPointerEventTargetByComponent()</a></li><li><a href="Crafty-keyboardDispatch.html">Crafty.keyboardDispatch()</a></li><li><a href="Crafty-keydown.html">Crafty.keydown</a></li><li><a href="Crafty-keys.html">Crafty.keys</a></li><li><a href="Crafty-lastEvent.html">Crafty.lastEvent</a></li><li><a href="Crafty-mouseButtons.html">Crafty.mouseButtons</a></li><li><a href="Crafty-mouseDispatch.html">Crafty.mouseDispatch()</a></li><li><a href="Crafty-mouseWheelDispatch.html">Crafty.mouseWheelDispatch()</a></li><li><a href="Crafty-multitouch.html">Crafty.multitouch()</a></li><li><a href="Crafty-selected.html">Crafty.selected</a></li><li><a href="Crafty-touchDispatch.html">Crafty.touchDispatch()</a></li><li><a href="Keyboard.html">Keyboard</a></li><li><a href="Mouse.html">Mouse</a></li><li><a href="MouseDrag.html">MouseDrag</a></li><li><a href="Touch.html">Touch</a></li></ul></li><li class="category">Misc<ul class="category-list"><li><a href="Crafty-addEvent.html">Crafty.addEvent()</a></li><li><a href="Crafty-device.html">Crafty.device</a></li><li><a href="Crafty-removeEvent.html">Crafty.removeEvent()</a></li><li><a href="Crafty-support.html">Crafty.support</a></li></ul></li><li class="category">Model<ul class="category-list"><li><a href="Model.html">Model</a></li></ul></li><li class="category">Scenes<ul class="category-list"><li><a href="Crafty-scene.html">Crafty.scene()</a></li></ul></li><li class="category">Stage<ul class="category-list"><li><a href="Crafty-background.html">Crafty.background()</a></li><li><a href="Crafty-scene.html">Crafty.scene()</a></li><li><a href="Crafty-viewport.html">Crafty.viewport</a></li></ul></li><li class="category">Utilities<ul class="category-list"><li><a href="Crafty-math.html">Crafty.math</a></li><li><a href="Delay.html">Delay</a></li><li><a href="Storage.html">Storage</a></li></ul></li></ul></div><div id="doc-content" class="doc-page-holder"><div class="doc-page"><h1>Crafty.map <span></span></h1><div class="doclet" id="Crafty-map"><span class="markdown"><p>Functions related with querying entities.</p>
</span><div><h4>See Also</h4><ul class="see-also-list"><li><a href="Crafty-HashMap.html">Crafty.HashMap</a></li></ul></div></div><div><h4>Methods</h4><ul class="page-toc"><li><a href="#Crafty-map-boundaries">.boundaries()</a></li><li><a href="#Crafty-map-insert">.insert()</a></li><li><a href="#Crafty-map-refresh">.refresh()</a></li><li><a href="#Crafty-map-remove">.remove()</a></li><li><a href="#Crafty-map-search">.search()</a></li><li><a href="#Crafty-map-traverseRay">.traverseRay()</a></li></ul><div class="doclet" id="Crafty-map-boundaries"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.map.boundaries()</h2><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">Object </span><span class="sig-noun">Crafty.map.boundaries</span></span><span class="sig-seperator">()</span></code><dl class="parameter returns"><dt class="returns"> [Returns] </dt><dd><span class="markdown"><p>An object with the following structure, which represents an MBR which contains all entities</p>
</span></dd></dl></div><span class="markdown"><p>Note that the returned object is a reference to the internally used object.
Use <code>Crafty.clone</code> to get a copy instead.</p>
<pre><code class="hljs ">{
  <span class="hljs-attr">min</span>: {
    <span class="hljs-attr">x</span>: val_x,
    <span class="hljs-attr">y</span>: val_y
  },
  <span class="hljs-attr">max</span>: {
    <span class="hljs-attr">x</span>: val_x,
    <span class="hljs-attr">y</span>: val_y
  }
}</code></pre></span></div><div class="doclet" id="Crafty-map-insert"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.map.insert()</h2><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">Object </span><span class="sig-noun">Crafty.map.insert</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">Object </span><span class="sig-noun">obj</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> obj </dt><dd><span class="markdown"><p>An entity to be inserted.</p>
</span></dd></dl><dl class="parameter returns"><dt class="returns"> [Returns] </dt><dd><span class="markdown"><p>An object representing this object&#39;s entry in the HashMap</p>
</span></dd></dl></div><span class="markdown"><p><code>obj</code> is inserted in &#39;.map&#39; of the corresponding broad phase cells. An object of the following fields is returned.</p>
<pre><code class="hljs ">{
  <span class="hljs-attr">keys</span>: the object that keep track <span class="hljs-keyword">of</span> cells
  obj: The inserted object
  map: the HashMap object
}</code></pre></span></div><div class="doclet" id="Crafty-map-refresh"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.map.refresh()</h2><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">void </span><span class="sig-noun">Crafty.map.refresh</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">Entry </span><span class="sig-noun">entry</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> entry </dt><dd><span class="markdown"><p>An entry to update</p>
</span></dd></dl></div><span class="markdown"><p>Update an entry&#39;s keys, and its position in the broad phrase map.</p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs ">Crafty.map.refresh(e);</code></pre></span></div></div><div class="doclet" id="Crafty-map-remove"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.map.remove()</h2><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">void </span><span class="sig-noun">Crafty.map.remove</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">Entry </span><span class="sig-noun">entry</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> entry </dt><dd><span class="markdown"><p>An entry to remove from the hashmap</p>
</span></dd></dl></div><span class="markdown"><p>Remove an entry from the broad phase map.</p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs ">Crafty.map.remove(e);</code></pre></span></div></div><div class="doclet" id="Crafty-map-search"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.map.search()</h2><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">Object </span><span class="sig-noun">Crafty.map.search</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">Object </span><span class="sig-noun">rect</span></span><span class="sig-seperator">[,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-qualifier">Boolean </span><span class="sig-noun">filter</span></span><span class="sig-seperator">])</span></code><dl class="parameter"><dt> rect </dt><dd><span class="markdown"><p>the rectangular region to search for entities.               This object must contain the properties <code>_x</code>,<code>_y</code>,<code>_w</code>,<code>_h</code>.</p>
</span></dd></dl><dl class="parameter"><dt> filter </dt><dd><span class="markdown"><p>If <code>false</code>, only performs a broad-phase collision check.  The default value is <code>true</code>.</p>
</span></dd></dl><dl class="parameter returns"><dt class="returns"> [Returns] </dt><dd><span class="markdown"><p>an (possibly empty) array of entities that have been found in the given region</p>
</span></dd></dl></div><span class="markdown"><p>Search for entities in the given region, using their broadphase bounding rectangles.</p>
<ul>
<li>If <code>filter</code> is <code>false</code>, just search for all the entries in the give <code>rect</code> region by broad phase collision. Entity may be returned duplicated.</li>
<li>If <code>filter</code> is <code>true</code>, filter the above results by checking that they actually overlap <code>rect</code>.</li>
</ul>
<p>The easier usage is with <code>filter == true</code>. For performance reason, you may use <code>filter == false</code>, and filter the result yourself. See examples in drawing.js and collision.js.</p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs "><span class="hljs-comment">// search for entities located in the current visible region of the viewport</span>
<span class="hljs-keyword">var</span> results = Crafty.map.search(Crafty.viewport.rect());
<span class="hljs-comment">// iterate over all those entities</span>
<span class="hljs-keyword">var</span> ent;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>, l = results.length; i &lt; l; ++i) {
    <span class="hljs-comment">// do something with an entity</span>
    ent = results[i];
    Crafty.log(<span class="hljs-string">'Found entity with id'</span>, ent.getId());
}</code></pre></span></div></div><div class="doclet" id="Crafty-map-traverseRay"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">Crafty.map.traverseRay()</h2><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">void </span><span class="sig-noun">Crafty.map.traverseRay</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">Object </span><span class="sig-noun">origin</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-qualifier">Object </span><span class="sig-noun">direction</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-qualifier">Function </span><span class="sig-noun">callback</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> origin </dt><dd><span class="markdown"><p>the point of origin from which the ray will be cast. The object must contain the properties <code>_x</code> and <code>_y</code>.</p>
</span></dd></dl><dl class="parameter"><dt> direction </dt><dd><span class="markdown"><p>the direction the ray will be cast. It must be normalized. The object must contain the properties <code>x</code> and <code>y</code>.</p>
</span></dd></dl><dl class="parameter"><dt> callback </dt><dd><span class="markdown"><p>a callback that will be called for each object that is encountered along the ray.                   This function is called with two arguments: The first one represents the object encountered;                   the second one represents the distance up to which all objects have been reported so far.                   The callback can return a truthy value in order to stop the traversal early.</p>
</span></dd></dl></div><span class="markdown"><p>Traverse the spatial map in the direction of the supplied ray.</p>
<p>Given the <code>origin</code> and <code>direction</code> the ray is cast and the <code>callback</code> is called
for each object encountered in map cells traversed by the ray.</p>
<p>The callback is called for each object that may be intersected by the ray.
Whether an actual intersection occurs shall be determined by the callback&#39;s implementation.</p>
</span><div class="example"><h4>Example</h4><span class="markdown"><pre><code class="hljs ">Crafty.e(<span class="hljs-string">"2D"</span>)
      .setName(<span class="hljs-string">'First entity'</span>)
      .attr({<span class="hljs-attr">x</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">w</span>: <span class="hljs-number">10</span>, <span class="hljs-attr">h</span>: <span class="hljs-number">10</span>});

Crafty.e(<span class="hljs-string">"2D"</span>)
      .setName(<span class="hljs-string">'Second entity'</span>)
      .attr({<span class="hljs-attr">x</span>: <span class="hljs-number">20</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">20</span>, <span class="hljs-attr">w</span>: <span class="hljs-number">10</span>, <span class="hljs-attr">h</span>: <span class="hljs-number">10</span>});

<span class="hljs-keyword">var</span> origin = {<span class="hljs-attr">_x</span>: <span class="hljs-number">-25</span>, <span class="hljs-attr">_y</span>: <span class="hljs-number">-25</span>};
<span class="hljs-keyword">var</span> direction = <span class="hljs-keyword">new</span> Crafty.math.Vector2D(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>).normalize();

Crafty.map.traverseRay(origin, direction, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">ent, processedDistance</span>) </span>{
  Crafty.log(<span class="hljs-string">'Encountered entity named'</span>, ent.getName()); <span class="hljs-comment">// logs 'First entity'</span>
  Crafty.log(<span class="hljs-string">'All entities up to'</span>, processedDistance, <span class="hljs-string">'px away have been reported thus far.'</span>);
  Crafty.log(<span class="hljs-string">'Stopping traversal after encountering the first entity.'</span>);
  <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
});</code></pre></span></div></div></div></div></div></div>
			
		</div>
	</div>
	<div class="clearer"></div>
	<div id="footer">
		<div id="contact">
			<a href="https://groups.google.com/forum/#!forum/craftyjs">
				<img src="../images/google.png" />
				google groups</a>
			<a href="mailto:starwed@gmail.com">
				<img src="../images/email.png" />
				starwed@gmail.com</a>
			<a href="https://github.com/craftyjs/Crafty">
				<img src="../images/github.png" />
				github</a>
		</div>
		<ul>
			<li><a href="../">首页</a></li>
				<li><a href="../getting-started/">快速开始</a></li>
				<li><a href="../documentation/">文档</a></li>
				<li><a href="../api/">API</a></li>
				<li><a href="../components/">组件</a></li>
				<li class="emph"><a href="../#install">下载</a></li>
		</ul>
		<p>
			&copy; Crafty 2010-2015. Crafty is distributed under the <a href="http://en.wikipedia.org/wiki/MIT_License">MIT License</a>.
		</p>
	</div>
</div>
</body>
</html>
